package com.g4.shoppingback.service.impl;

import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.mapper.RoleMapper;
import com.g4.shoppingback.mapper.UserRoleMapper;
import com.g4.shoppingback.pojo.DdUsersInfo;
import com.g4.shoppingback.mapper.DdUsersInfoMapper;
import com.g4.shoppingback.pojo.Role;
import com.g4.shoppingback.pojo.UserRole;
import com.g4.shoppingback.service.IDdUsersInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@Service
public class DdUsersInfoServiceImpl extends ServiceImpl<DdUsersInfoMapper, DdUsersInfo> implements IDdUsersInfoService {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private DdUsersInfoMapper ddUsersInfoMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;



    /**
     * 用户登陆查询
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public DdUsersInfo selectUser(String username) {
        return ddUsersInfoMapper.selectUser(username);
    }

    /**
     * 用户权限查询
     * @param id 用户id
     * @return 权限列表
     */
    @Override
    public List<String> selectPermissions(Integer id) {
        return ddUsersInfoMapper.selectPermissions(id);
    }

    /**
     * 根据id查询用户和角色(修改用户信息要用)
     * @param id 传入用户id
     * @return 返回单个用户信息
     */
    @Override
    public DdUsersInfo selectUserById(Integer id) {
        DdUsersInfo ddUsersInfo = ddUsersInfoMapper.selectUserById(id);
        Role role = roleMapper.selectRoleByUserId(id);
        ddUsersInfo.setRoleId(role.getId());
        return ddUsersInfo;
    }

    /**
     * 根据分页信息或用户名查询用户
     * @param pageVo 封装了查询关键字和页数
     * @return 返回分页信息
     */
    @Override
    public PageVo selectUserByPaged(PageVo pageVo) {
        Integer pageSize = 5;   //设置每页固定显示5条数据
        Integer pageStart = (pageVo.getPageNum()-1)*pageSize;   //设置从第几条开始
        Long total = ddUsersInfoMapper.selectTotal(pageVo.getQuery());  //查询总条数
        List<DdUsersInfo> usersInfos = ddUsersInfoMapper.selectUserByPaged(pageVo.getQuery(),pageStart,pageSize);
        for (DdUsersInfo usersInfo : usersInfos) {
            Role role = roleMapper.selectRoleByUserId(usersInfo.getId());
            usersInfo.setRoleName(role.getName());
        }
        pageVo.setPageSize(pageSize).setTotal(total).setRows(usersInfos); //封装分页信息
        return pageVo;
    }

    /**
     * 根据用户id删除用户
     * @param id 用户id
     * @return 返回影响行数
     */
    @Transactional
    @Override
    public int deleteUserById(Integer id) {
        int row = userRoleMapper.deleteRoleByUserId(id);  //删除用户角色关系
        if(row==1){
            row = ddUsersInfoMapper.deleteUserById(id);    //删除用户
            if(row==1) return 1;
        }
        return 0;
    }

    /**
     * 新增用户
     * @param ddUsersInfo 用户信息
     * @return 返回影响行数
     */
    @Transactional
    @Override
    public int addUser(DdUsersInfo ddUsersInfo) {
        String encodePassword = bCryptPasswordEncoder.encode(ddUsersInfo.getPassword());
        ddUsersInfo.setPassword(encodePassword);
        int row = ddUsersInfoMapper.insert(ddUsersInfo); //添加用户
        if(row==1){
            row = userRoleMapper.addRole(ddUsersInfo.getId(),ddUsersInfo.getRoleId()); //添加用户角色关系
            if(row==1) return 1;
        }return 0;
    }

    /**
     * 校验用户名和电话是否重复
     * @param username 用户名
     * @param telephone 电话
     * @return 返回用户数据
     */
    @Override
    public String checkNameAndPhone(String username, String telephone) {
        List<String> usernames = ddUsersInfoMapper.checkName(username);
        List<String> telephones =  ddUsersInfoMapper.checkPhone(telephone);
        if(usernames.size()!=0){
            return "该用户已被注册!";
        }
        if(telephones.size()!=0){
            return "该手机号已被使用!";
        }
        return null;
    }

    /**
     * 修改用户信息
     * @param ddUsersInfo
     * @return 返回影响行数
     */
    @Transactional
    @Override
    public int updateUser(DdUsersInfo ddUsersInfo) {
        DdUsersInfo user = ddUsersInfoMapper.selectUserById(ddUsersInfo.getId());
        if(ddUsersInfo.getUsername()==user.getUsername()){ //当用户名不修改时,设置为null
            ddUsersInfo.setUsername(null);
        }
        if(ddUsersInfo.getTelephone()==user.getTelephone()){ //当电话不修改时,设置为null
            ddUsersInfo.setTelephone(null);
        }
        int row = ddUsersInfoMapper.updateUser(ddUsersInfo); //修改
        if(row==1){
            UserRole userRole = new UserRole(null, ddUsersInfo.getId(), ddUsersInfo.getRoleId());
            row =userRoleMapper.updateUserRole(userRole);
            if(row==1)return 1;
        }
        return 0;
    }

    /**
     * 修改用户密码
     * @param id 用户id
     * @param newPassword 新密码
     * @return 返回影响行数
     */
    @Override
    public int updatePassword(Integer id, String newPassword) {
        return ddUsersInfoMapper.updatePassword(id,newPassword);
    }

}
